package com.zjeav.lingjiao.ui.presenter;

import com.zjeav.lingjiao.base.baseBean.Book;
import com.zjeav.lingjiao.base.baseBean.Province;
import com.zjeav.lingjiao.base.baseBean.Result;
import com.zjeav.lingjiao.base.listener.CommonListener;
import com.zjeav.lingjiao.base.request.AuthorizeCard;
import com.zjeav.lingjiao.base.request.BookAuthorizeRequest;
import com.zjeav.lingjiao.base.request.FreePay;
import com.zjeav.lingjiao.base.request.MallBookRequest;
import com.zjeav.lingjiao.base.request.OrderQuery;
import com.zjeav.lingjiao.base.response.BooksResponse;
import com.zjeav.lingjiao.base.response.OrderQueryResponse;
import com.zjeav.lingjiao.base.response.SubjectResponse;
import com.zjeav.lingjiao.base.service.AddFavoriteService;
import com.zjeav.lingjiao.base.service.BookAuthorizeService;
import com.zjeav.lingjiao.base.service.BookGradeService;
import com.zjeav.lingjiao.base.service.BookSubjectService;
import com.zjeav.lingjiao.base.service.FavoriteAudioService;
import com.zjeav.lingjiao.base.service.FreePayService;
import com.zjeav.lingjiao.base.service.MallBookService;
import com.zjeav.lingjiao.base.service.PayQueryService;
import com.zjeav.lingjiao.base.service.RegionService;
import com.zjeav.lingjiao.base.service.RemoveFavoriteAduioService;
import com.zjeav.lingjiao.base.service.RemoveFavoriteService;
import com.zjeav.lingjiao.base.service.ValidAuthorizeCardService;
import com.zjeav.lingjiao.base.tools.ObservableServiceTool;
import com.zjeav.lingjiao.base.tools.RetrofitInstance;
import com.zjeav.lingjiao.ui.book.BookDetailContract;
import com.zjeav.lingjiao.ui.book.MallBookContract;
import com.zjeav.lingjiao.ui.book.SaomiaoBookContract;

import java.util.ArrayList;

import io.reactivex.Observable;
import retrofit2.Retrofit;

public class BookPresenter implements CommonListener {
    private SaomiaoBookContract.View view;
    private MallBookContract.View mallBookView;
    private BookDetailContract.BookMetaCollectView bookMetaCollectView;
    private BookDetailContract.BookCollectView bookCollectView;
    private BookDetailContract.PayView payView;

    public BookPresenter(BookDetailContract.PayView payView) {
        this.payView = payView;
    }

    public BookPresenter(BookDetailContract.BookMetaCollectView bookMetaCollectView) {
        this.bookMetaCollectView = bookMetaCollectView;
    }

    public BookPresenter(BookDetailContract.BookCollectView bookCollectView) {
        this.bookCollectView = bookCollectView;
    }

    public BookPresenter(SaomiaoBookContract.View view) {
        this.view = view;
    }

    public BookPresenter(MallBookContract.View view) {
        this.mallBookView = view;
    }

    public void getSaoMiaoResult(AuthorizeCard authorizeCard) {
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        ValidAuthorizeCardService service = retrofit.create(ValidAuthorizeCardService.class);
        ObservableServiceTool.getResult((Observable) service.validAuthorizeCard(authorizeCard), 0, this);

    }

    public void getFiltrateBooks(MallBookRequest request) {
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        MallBookService service = retrofit.create(MallBookService.class);
        ObservableServiceTool.getResult((Observable) service.getMallBooks(request), 1, this);
    }

    public void getGrades() {
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        BookGradeService service = retrofit.create(BookGradeService.class);
        ObservableServiceTool.getResult((Observable) service.getBookGrade(), 2, this);
    }

    public void getSubjects() {
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        BookSubjectService service = retrofit.create(BookSubjectService.class);
        ObservableServiceTool.getResult((Observable) service.getSubjects(), 3, this);
    }

    public void getRegions() {
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        RegionService service = retrofit.create(RegionService.class);
        ObservableServiceTool.getResult((Observable) service.getReions(), 11, this);
    }

    public void addBook(BookAuthorizeRequest request) {
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        BookAuthorizeService service = retrofit.create(BookAuthorizeService.class);
        ObservableServiceTool.getResult((Observable) service.addBook(request), 4, this);
    }

    public void collectBookMeta(int id) {
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        FavoriteAudioService service = retrofit.create(FavoriteAudioService.class);
        ObservableServiceTool.getResult((Observable) service.addFavoriteAudio(id), 5, this);
    }

    public void removeCollectBookMeta(int id) {
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        RemoveFavoriteAduioService service = retrofit.create(RemoveFavoriteAduioService.class);
        ObservableServiceTool.getResult((Observable) service.removeFavoriteAdio(id), 6, this);
    }

    public void collectBook(int id) {
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        AddFavoriteService service = retrofit.create(AddFavoriteService.class);
        ObservableServiceTool.getResult((Observable) service.addFavorite(id), 7, this);
    }

    public void removeCollectBook(int id) {
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        RemoveFavoriteService service = retrofit.create(RemoveFavoriteService.class);
        ObservableServiceTool.getResult((Observable) service.removeFavorite(id), 8, this);
    }

    public void freePay(FreePay pay) {
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        FreePayService service = retrofit.create(FreePayService.class);
        ObservableServiceTool.getResult((Observable) service.freePay(pay), 9, this);
    }
    public void queryOrder(OrderQuery orderQuery){
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        PayQueryService service = retrofit.create(PayQueryService.class);
        ObservableServiceTool.getResult((Observable) service.getOrderInfo(orderQuery), 10, this);
    }

    @Override
    public void OnSuccess(Result result, int type) {
        if (type == 0) {
            if (result.getCode() == 0) {
                this.view.showSaomiaoResult((Book) result.getData());
            } else {
                this.view.showError(null, result.getMessage());
            }

        } else if (type == 1) {
            BooksResponse response = (BooksResponse) result.getData();
            this.mallBookView.showMallBooks(response.getData());
        } else if (type == 2) {
            ArrayList<SubjectResponse> list = (ArrayList<SubjectResponse>) result.getData();
            this.mallBookView.getGrades(list);
        } else if (type == 3) {
            ArrayList<SubjectResponse> list = (ArrayList<SubjectResponse>) result.getData();
            this.mallBookView.getSubjects(list);
        } else if (type == 4) {
            if (result.getCode() == 0) {
                this.view.addBook(result);
            } else {
                this.view.addFail(null, result.getMessage());
            }
        } else if (type == 5) {
            if (result.getCode() == 0) {
                this.bookMetaCollectView.CollectSuccess(result);
            } else {
                this.bookMetaCollectView.ShowError(null, result.getMessage());
            }
        } else if (type == 6) {
            if (result.getCode() == 0) {
                this.bookMetaCollectView.RemoveCollect(result);
            } else {
                this.bookMetaCollectView.ShowError(null, result.getMessage());
            }
        } else if (type == 7) {
            if (result.getCode() == 0) {
                this.bookCollectView.BookCollectSuccess(result);
            } else {
                this.bookCollectView.ShowError(null, result.getMessage());
            }
        } else if (type == 8) {
            if (result.getCode() == 0) {
                this.bookCollectView.RemoveBookCollect(result);
            } else {
                this.bookCollectView.ShowError(null, result.getMessage());
            }
        } else if (type == 9) {
            if (result.getCode() == 0) {
                this.bookCollectView.FreePay(result);
            } else {
                this.bookCollectView.ShowError(null, result.getMessage());
            }
        }else if (type==10){
            if (result.getCode()==0){
                this.payView.QueryPay((OrderQueryResponse) result.getData());
            }
        }else if (type ==11){
            ArrayList<Province> province = (ArrayList<Province>) result.getData();
            this.mallBookView.showRegions(province);
        }

    }

    @Override
    public void OnFailure(Throwable throwable, int type) {
        if (type == 0) {
            this.view.showError(throwable, "");
        } else if (type == 1) {
            this.mallBookView.showError(throwable);
        } else if (type == 2) {
            this.mallBookView.showError(throwable);
        } else if (type == 3) {
            this.mallBookView.showError(throwable);
        } else if (type == 4) {
            this.view.showError(throwable, "");
        } else if (type == 5) {
            this.bookMetaCollectView.ShowError(throwable, "");
        } else if (type == 6) {
            this.bookMetaCollectView.ShowError(throwable, "");
        } else if (type == 7) {
            this.bookCollectView.ShowError(throwable, "");
        } else if (type == 8) {
            this.bookCollectView.ShowError(throwable, "");
        } else if (type == 9) {
            this.bookCollectView.ShowError(throwable, "");
        }
    }
}
